import { registerClass } from "../../../cfw/tools/class_decorator";
import { ResultCallback, ResultState } from "../../SDKDefine";
import SDKHelper from "../../tools/SDKHelper";
import { sdk_engine } from "../../tools/sdk_engine";
import { SDKSystem } from "./SDKSystem";


@registerClass('SDKWebSystem')
export class SDKWebSystem extends SDKSystem {
    request(url: string, func: Function) {
        SDKHelper.sendHttpRequest(url, '', (msg: string, data) => {
            func(msg, data)
        })
    }

    canInstallShortcut(callback: ResultCallback) {
        callback(ResultState.YES)
    }

    installShortcut(result: ResultCallback) {
        result(ResultState.YES)
    }

    hasVibration() {
        return false;
    }

    exitApplication(): void {
        sdk_engine.exit();
    }

    saveFile(filename: string, content: string) {
        // 创建一个Blob实例，类型为octet/stream
        var blob = new Blob([content], { type: 'application/json;charset=utf-8' });
        // 创建一个指向Blob对象的URL
        var url = URL.createObjectURL(blob);

        // 创建一个a标签
        var link = document.createElement('a');
        link.href = url;
        link.download = filename;

        // 触发下载
        document.body.appendChild(link);
        link.click();

        // 清理并移除元素和URL引用
        document.body.removeChild(link);
        URL.revokeObjectURL(url);
    }
}